/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibusplus.com.reportRequest.clases;

import com.jaspersoft.jrx.export.JRTxtExporter;
import com.jaspersoft.jrx.export.JRTxtExporterParameter;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author solman
 */
public class ReportExport {

    private String clientFolder;
    private String xml_uri;
    private String report;
    private String format;
    private String xpath;
    private Properties parametros = new Properties();

    public ReportExport() {
    }

    public void setClientFolder(String clientFolder) {
        this.clientFolder = clientFolder;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public void setReport(String report) {
        this.report = report;
    }

    public void setXml_uri(String xml_uri) {
        this.xml_uri = xml_uri;
    }

    public void setXpath(String xpath) {
        this.xpath = xpath;
    }

    private HttpServletRequest getRequest() {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
        return request;
    }

    public String getTEMP_DIR() {
        FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) fc.getExternalContext().getContext();
        String rutaTemp = servletContext.getRealPath(".temp".replace(".", SOUtil.getSeparadorArchivos()));
        System.out.println("Report Server Path:" + rutaTemp);
        rutaTemp = rutaTemp + SOUtil.getSeparadorArchivos();
        return rutaTemp;
    }

    public String getTempFileName() {
        SimpleDateFormat sp = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
        return sp.format(new Date());
    }

    private void getConnectionProperties() throws IOException {
        parametros.load(this.getClass().getResourceAsStream("reportRequest.properties"));
    }

    public String getREPORTS_PATH() {
        return parametros.getProperty("REPORTS_PATH");
    }

    public String getURLReport() {
        StringBuilder sb = new StringBuilder();
        try {
            getConnectionProperties();
            if (getREPORTS_PATH().trim().equals("")) {
                throw new Exception("The REPORTS_PATH parameter is missing.");
            }
            String reportPath = getREPORTS_PATH() + clientFolder + "\\" + report + ".jasper";
            JRXmlDataSource xmlDataSource = new JRXmlDataSource(JRLoader.getLocationInputStream(xml_uri), xpath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap(), xmlDataSource);
            JRExporter exporter = null;
            if (format.equals("pdf")) {
                exporter = new JRPdfExporter();
            } else if (format.equals("xls")) {
                exporter = new JRXlsExporter();
                exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
                exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
                exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
            } else if (format.equals("rtf")) {
                exporter = new JRRtfExporter();
            } else if (format.equals("docx")) {
                exporter = new JRDocxExporter();
            } else if (format.equals("txt")) {
                exporter = new JRCsvExporter();
                exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",");
                exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER,"\n");
                exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"ISO-8859-1");
            } else if (format.equals("html")) {
                exporter = new JRHtmlExporter();
                exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
                exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "<div style=\"display:none;\">Esto no debe verse</div>");
                exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
                exporter.setParameter(JRHtmlExporterParameter.ZOOM_RATIO, Float.parseFloat("1.5"));

            }
            String fileName = report + "_" + getTempFileName() + "." + format;
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new File(getTEMP_DIR() + fileName));
            exporter.exportReport();
            sb.append(getRequest().getContextPath());
            sb.append("/faces/temp/").append(fileName);
        } catch (Exception e) {
            sb.setLength(0);
            sb.append(getRequest().getContextPath());
            sb.append("/faces/error.jsp?errorMessage=").append(e.getMessage());
        }
        return sb.toString();
    }
}
